//
//  PreferenceSettingsManager.swift
//  WatermarkCamera
//
//  Created by 赵新 on 8/7/2024.
//

import RxSwift
import Foundation
import AxcBedrock

class PreferenceSettingsManager: NSObject {
    /// 单例实例化
    static let shared: PreferenceSettingsManager = {
        let manager = PreferenceSettingsManager()
        manager.loadCameraSettings() // 加载相机设置
        return manager
    }()

    /// 默认相机设置对象
    private(set) lazy var cameraSetting: CameraSettingsModel = .init()

    /// 更新相机设置
    static func updateCameraSettings(model: CameraSettingsModel) {
        let json = model.toDictionary
        if !json.isEmpty {
            shared.cameraSetting = model
            UserDefaults.standard.set(json, forKey: "cameraSetting")
        }
    }

    /// 加载相机设置
    func loadCameraSettings() {
        if let _cameraSettingJson = UserDefaults.standard.object(forKey: "cameraSetting") as? [String: Any],
           let data = try? JSONSerialization.data(withJSONObject: _cameraSettingJson),
           let model: CameraSettingsModel = try? JSONDecoder().decode(CameraSettingsModel.self, from: data) {
            cameraSetting = model
        }
    }
}
